//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.core.io;

import java.io.Writer;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jfab.core.collection.CommentList;
import org.jfab.core.collection.MethodBodyList;
import org.jfab.core.collection.ModifierList;
import org.jfab.core.factory.StringFactory;
import org.jfab.core.model.CGInitializationBlock;

/**
 * Provides an initializer block writer.
 */
public class CGInitializationBlockWriter
{
    /** Basic code generator. */
    private final BasicCodeGenerator _basicCodeGenerator;

    /** String factory. */
    private final StringFactory _stringFactory;

    /**
     * Construct this object with the given parameters.
     * 
     * @param stringFactory String factory.
     * @param basicCodeGenerator Basic code generator.
     */
    public CGInitializationBlockWriter(final StringFactory stringFactory,
            final BasicCodeGenerator basicCodeGenerator)
    {
        _stringFactory = stringFactory;
        _basicCodeGenerator = basicCodeGenerator;
    }

    /**
     * Write an initialization block.
     * 
     * @param writer Writer.
     * @param initializationBlock Code generator initialization block.
     */
    public final void write(final Writer writer,
            final CGInitializationBlock initializationBlock)
    {
        Validate.notNull(initializationBlock, "initializationBlock is null");

        final CommentList commentLines = new CommentList(
                initializationBlock.getCommentLines());
        final ModifierList modifiers = new ModifierList(
                initializationBlock.getModifiers());
        final String modifiersString = getStringFactory()
                .createModifiersString(modifiers);
        final MethodBodyList methodBodyLines = new MethodBodyList(
                initializationBlock.getMethodBodyLines());

        getBasicCodeGenerator().writeMultilineJavadocComment(writer,
                commentLines);

        if (StringUtils.isNotEmpty(modifiersString))
        {
            getBasicCodeGenerator().writeLine(writer, modifiersString);
        }

        getBasicCodeGenerator().writeOpenBrace(writer);
        writeMethodBody(writer, methodBodyLines);
        getBasicCodeGenerator().writeCloseBrace(writer);
    }

    /**
     * @return the basicCodeGenerator
     */
    private BasicCodeGenerator getBasicCodeGenerator()
    {
        return _basicCodeGenerator;
    }

    /**
     * @return the stringFactory
     */
    private StringFactory getStringFactory()
    {
        return _stringFactory;
    }

    /**
     * @param writer Writer.
     * @param methodBodyLines Method body lines.
     */
    private void writeMethodBody(final Writer writer,
            final MethodBodyList methodBodyLines)
    {
        if (CollectionUtils.isNotEmpty(methodBodyLines))
        {
            final BasicCodeGenerator basicCodeGenerator = getBasicCodeGenerator();
            final int size = methodBodyLines.size();

            for (int i = 0; i < size; i++)
            {
                basicCodeGenerator.writeLine(writer, methodBodyLines.get(i));
            }
        }
    }
}
